﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using rFDevLib;

namespace LapSim {
    struct Waypoint {
        public Vector pos, perp, normal;
        public double pathDry, pathWet;
        public double roadLeft, roadRight;
        public int next, prev;

        public Vector GetPathPos( double water = 0.0 ) {
            double path = (pathDry * (1 - water)) + (pathWet * water);

            return pos + (perp * path);
        }

        public Vector GetRoadLeft() {
            return pos - (perp * roadLeft);
        }

        public Vector GetRoadRight() {
            return pos + (perp * roadRight);
        }
    }
    
    class TrackData {
        public List<Waypoint> m_waypoints;

        public TrackData() {
        }

        public TrackData( string fileName ) {
            ParseFile( fileName );
        }

        public void ParseFile( string fileName ) {
            rFDataFile data = new rFDataFile( fileName );
            m_waypoints = new List<Waypoint>();
            
            Waypoint wp = new Waypoint();
            bool first = true;
            foreach ( rFParam a in data.m_params ) {
                if ( a.section == "Waypoint" ) {
                    if ( a.name == "wp_pos" ) {
                        if ( !first )
                            m_waypoints.Add( wp );
                        first = false;

                        wp = new Waypoint();
                        wp.pos = new Vector( a.GetDouble( 0 ), a.GetDouble( 1 ), a.GetDouble( 2 ) );
                    } else {
                        switch ( a.name ) {
                            case "wp_perp":
                                wp.perp = new Vector( a.GetDouble( 0 ), a.GetDouble( 1 ), a.GetDouble( 2 ) );
                                break;
                            case "wp_path":
                                wp.pathDry = a.GetDouble( 0 );
                                wp.pathWet = a.GetDouble( 1 );
                                break;
                            case "wp_normal":
                                wp.normal = new Vector( a.GetDouble( 0 ), a.GetDouble( 1 ), a.GetDouble( 2 ) );
                                break;
                            case "wp_width":
                                wp.roadLeft = a.GetDouble( 0 );
                                wp.roadRight = a.GetDouble( 1 );
                                break;
                            case "WP_PTRS":
                                wp.prev = a.GetInt( 0 );
                                wp.next = a.GetInt( 1 );
                                break;
                        }
                    }
                }
            }

            return;
        }
    }
}
